<?php
namespace core;

class mzl {
  public static $classMap=array();
  public $assign;
  static public function run(){
    $route=new \core\lib\route();
    $ctrl=$route->ctrl;
    $action=$route->action;
    $ctrlfile=APP.'/ctrl/'.$ctrl.'Ctrl.php';
    $ctrlClass=MODULE.'\ctrl\\'.$ctrl.'Ctrl';
    if(is_file($ctrlfile)){
      include $ctrlfile;
      $ctrl=new $ctrlClass();
      $ctrl->$action();
    }else {
      throw new \Exception('控制器'.$ctrl.'不存在');
    }
  }

  static public function load($class){
    //自动加载类库
    $class=str_replace('\\','/',$class);
    if(isset($classMap[$class])){
      return true;
    }else{
      $file=MZL.'/'.$class.'.php';
      if(is_file($file)){
        include $file;
        self::$classMap[str_replace('core/','',$class)]=$class;
      }else {
        return false;
      }
    }

  }

  public function assign($name,$value){
    $this->assign[$name]=$value;
  }

  public function display($filename){
    $file=APP.'/views/'.$filename;
    if(is_file($file)){
      // \Twig_autoloader::register();
      $loader = new \Twig_Loader_Filesystem(APP.'/views');
      $twig = new \Twig_Environment($loader, array(
          'cache' => APP.'/caches',
          'debug'=>DEBUG
      ));
      $template=$twig->loadTemplate($filename);
      $template->display($this->assign?$this->assign:array());
    }
  }
}
